var listColumns = [[
    {field: "id", checkbox: true},
    {field: "createMobile", title: "报修人电话", align: "center", width: 100},
    {field: "creatorName", title: "报修人", align: "center", width: 100},
    {field: "source", title: "工单来源", align: "center", width: 80,formatter:function (value) {
        switch (value){
            case "1": return "电话报修";
            case "2": return "邮件报修";
            case "3": return "微信报修";
            case "4": return "二维码";
            case "5": return "其他";
            default: return "";
        }
    }},
    {field: "machineType", title: "报修类型", align: "center", width: 50},
    {field: "callerDeptName", title: "报修人部门", align: "center", width: 120},
    {field: "address", title: "报修地点", align: "center", width: 80},
    {field: "processSpecification", title: "处理说明", align: "center", width: 150},
    {field: "currentName", title: "负责工程师", align: "center", width: 150},
    {field: "beginProcessTime", title: "开始处理时间", align: "center", width: 140},
    {field: "endProcessTime", title: "结束处理时间", align: "center", width: 140},
    {field: "status", title: "当前状态", align: "center", width: 80,formatter:function (value) {
        switch(value){
            // 01:调配 02:已接单 03:服务中 04:结束服务
            case "01": return "待调配";
            case "02": return "已接单";
            case "03": return "服务中";
            case "04": return "结束服务";
            case "05": return "已评价";
            case "06": return "待接单";
            default: return "";
        }
    }},
    {field: "createTime", title: "创建时间", align: "center", width: 140},
    {field: "updateTime", title: "末次处理时间", align: "center", width: 140}
]];

layui.config({
    base: "../static/agent-ui/js/"
});

$.fn.toJSON = function () {
    var o = {};
    var a = this.serializeArray();
    $.each(a, function () {
        var name = this.name;
        var value = this.value;
        var paths = this.name.split(".");
        var len = paths.length;
        var obj = o;
        $.each(paths, function (i, e) {
            if (i == len - 1) {
                if (obj[e]) {
                    if (!obj[e].push) {
                        obj[e] = [obj[e]];
                    }
                    obj[e].push(value || '');
                } else {
                    obj[e] = value || '';
                }
            } else {
                if (!obj[e]) {
                    obj[e] = {};
                }
            }
            obj = o[e];
        });
    });
    return o;
};

layui.use(["laypage", "layer", "validator", "tools", "form", "select", "laydate"], function () {
    var form = layui.form;
    var date = layui.laydate;
    date.render({
        elem: '#ringTime',
        type: 'datetime',
        range: true
    });


    var layer=layui.layer;

    $(function () {
        $('#list').datagrid({
            columns: listColumns,
            fitColumns: false,
            fit: true,
            singleSelect: true,
            pagination: true,
            pageSize: 30,
            pageList: [30, 50, 100, 1000],
            toolbar: '#receivedListHeader',
            method: 'get',
            url: ctx +'/report/list',
            onLoadSuccess: function () {
                getDept(function(optionString){
                    $("#search_form").find('select[name=callerDeptId]').empty();
                    $("#search_form").find('select[name=callerDeptId]').append(optionString);
                    var form = layui.form;
                    form.render('select');
                });

            }
        });
    });

    $('#search_submit').on('click', function () {
        $('#list').datagrid('load', $('#search_form').toJSON());
    });

})

function getDept(callback) {
        $.ajax({
            url: ctx + '/dept/list/nopage',
            type : 'GET',
            dataType: 'json',
            success: function (result) {
                var list = result.data;
                var optionString = "<option value=''>请选择...</option>";
                list.forEach((entry,index) =>{
                    optionString += "<option value = '"+entry.qywxId+"'>"+entry.deptName+"</option>";
            });
                if(callback !=null){
                    callback(optionString);
                }

            },
            error: function (result) {
                layer.open({
                    title: '报错了'
                    ,content: '获取部门失败'
                });
            }
        });
}



